
'''
        @author: msune,omoya,CarolinaFernandez
    @@organization: i2CAT, OFELIA FP7

    Persistence engine
    Implementes driver-based persistence backend selection
'''


class PersistenceEngine(object):

    #Default Class Attributes
    _defaultParser = "RegexParser"
    _defaultPersistence = "RAWFile"

    #Drivers
    _drivers = ["Django","RAWFile"]

    @staticmethod
    def _getDriver(driverName):
        if driverName == "Django":
            mod = __import__("pelib.persistence.backends.django.Django", fromlist=["Django"])
            return mod.Django
        elif driverName == "RAWFile":
            mod = __import__("pelib.persistence.backends.rawfile", fromlist=["RAWFile"])
            return mod.RAWFile
        else:
            raise Exception(driverName + ' not supported')

    @staticmethod
    def save(obj, pBackend, parser=None, **kwargs):
        return PersistenceEngine._getDriver(pBackend).save(obj, parser, **kwargs)

    @staticmethod
    def load(tableName, pBackend, resolverMappings, parser=None, **kwargs):
        return PersistenceEngine._getDriver(pBackend).load(tableName, resolverMappings, parser, **kwargs)

        '''
        Retrieves every Driver's PolicyRuleTable object for a given name.
        This method should be seldom used.
        '''
    @staticmethod
    def loadAll(tableName, pBackend):
        return PersistenceEngine._getDriver(pBackend).loadAll(tableName)

        '''
        Deletes a Driver's PolicyRuleTable object for a given ID.
        This method should be seldom used.
        '''
        @staticmethod
        def delete(tableID, pBackend):
                return PersistenceEngine._getDriver(pBackend).delete(tableID)

